unit oAgentLevel;

interface

uses uOSPABA;

type
  TAgentLevel = class(TAgent)
    private
    public
      constructor Create(paID: Cardinal; paMySim: TSimulation; paParent: tAgent);
  end;

  TManagerLevel = class(TManager)
   public
    procedure ProcessMessage(var paMessage: TMessageForm); override;
  end;

implementation

uses oSimulation, Dialogs, oEntityTypeConstants;

{ TAgentPoschodia }

constructor TAgentLevel.Create(paID: Cardinal; paMySim: TSimulation;
  paParent: tAgent);
begin
   inherited create(paID,paMySim,paParent);
   TManagerLevel.Create(IDManazerPoschodia,self,NoMOPN);
   AddOwnMessage(mcPrichodCP);
   AddOwnMessage(mcDone);
   AddOwnMessage(mcObsluzeny);
end;

{ TManazerPoschodia }

procedure TManagerLevel.ProcessMessage(var paMessage: TMessageForm);
var
  i: Integer;
begin
  with MySim as TPedSimulation do
  case paMessage.Code of

    mcPrichodCP: begin
      (MyAgent as TAgentLevel).AddActor((paMessage as TPSMessage).Actor);
      paMessage.Code := mcGoal;
      paMessage.Addressee := (paMessage as TPSMessage).Actor;
      goalActor(paMessage);
    end;

      mcDone:
      begin
        if (paMessage as TPSMessage).Actor.Target = nil
          then begin
            paMessage.Code := mcObsluzeny;
            Notice(paMessage);
          end else case (paMessage as TPSMessage).Actor.Target.Id of

            -1:
            {ET_TURNIQUET_TYPE:
            begin
              paMessage.Code := mcPrejstTurniket;
              paMessage.Addressee := (MySim as TPedSimulation).AgentTourniquet;
              Request(paMessage);
            end}

            else begin
              paMessage.Code := mcObsluzeny;
              Notice(paMessage);
            end;

          end;

      end;

    mcObsluzeny:
    begin
      if (paMessage as TPSMessage).Actor.Target <> nil
        then begin

          i := (paMessage as TPSMessage).Actor.ActId;
          if i = (paMessage as TPSMessage).Actor.Path.WayPoints.Count - 1
            then begin
              paMessage.Code := mcOdchodCP;
              paMessage.Addressee := AgentTerminal;
              (paMessage as TPSMessage).Actor.TickSubscriber := false;
              notice(paMessage);
            end else begin
              (paMessage as TPSMessage).Actor.GoAhead;
              if (paMessage as TPSMessage).Actor.Typ = atBA then
                (paMessage as TPSMessage).Actor.TargetMatrix := (paMessage as TPSMessage).Actor.Path.WayMatrixes.Items[i];
              paMessage.Code := mcGoal;
              paMessage.Addressee := (paMessage as TPSMessage).Actor;
              goalActor(paMessage);
            end;

        end else begin
          paMessage.Code := mcOdchodCP;
          paMessage.Addressee := AgentTerminal;
          (paMessage as TPSMessage).Actor.TickSubscriber := false;
          notice(paMessage);
        end;

    end;

  end;
end;

end.
